# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
import pymysql


class DoubanPipeline(object):
    def __init__(self):
        self.conn = pymysql.connect(
            host="localhost",
            user="root",
            password="hyg412727",  # 数据库密码
            charset="utf8mb4",  # 字段类型
            database="bole"  # 数据库名称，应与创建的一致
        )

    def process_item(self, item, spider):
        cursor = self.conn.cursor()
        sql = 'insert into douban_info (`title`,`url`,`comments`)'.format(item._value.get("name", "", ""),
                                                                          item._value.get("url", "", ""),
                                                                          item._value.get("comments", "", ""))
        cursor.execute(sql)
        self.conn.commit()
        cursor.close()
        return item
